package com.hjy.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hjy.common.utils.PageUtils;
import com.hjy.common.utils.Query;
import com.hjy.product.dao.AttrGroupDao;
import com.hjy.product.dao.CategoryDao;
import com.hjy.product.entity.AttrEntity;
import com.hjy.product.entity.CategoryEntity;
import com.hjy.product.service.AttrService;
import com.hjy.product.vo.AttrGroupAndAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hjy.product.entity.AttrGroupEntity;
import com.hjy.product.service.AttrGroupService;
import org.w3c.dom.ls.LSException;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        if (catId == -1) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), new QueryWrapper<AttrGroupEntity>());
            return new PageUtils(page);
        }else {
            // select * from pms_attr_group where catelog_id = catId and (attr_group_id = key or attr_group_name like %key%)
            LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AttrGroupEntity::getCatelogId,catId);
            if (params.get("key") != null && StrUtil.isNotBlank((String) params.get("key"))) {
                wrapper.and(wrapper1 -> {
                    String key = (String) params.get("key");
                    wrapper1.eq(AttrGroupEntity::getAttrGroupId,key).or().like(AttrGroupEntity::getAttrGroupName,key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 返回三级分类的名称ID序列
     * @param catelogId
     * @return
     */
    @Override
    public List<Long> getThreeFloorCatId(Long catelogId) {
        List<Long> list = new ArrayList<>();
        recursionGetData(catelogId,list);
        CollUtil.reverse(list);
        return list;
    }

    @Override
    public List<AttrGroupAndAttrsVo> getAttrGroupAndAttrs(Long catId) {
        // 1.首先查出所有的 group
        QueryWrapper<AttrGroupEntity> w = new QueryWrapper<>();
        w.eq("catelog_id",catId);
        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(w);
        List<AttrGroupAndAttrsVo> collect = attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrGroupAndAttrsVo attrGroupAndAttrsVo = new AttrGroupAndAttrsVo();
            BeanUtil.copyProperties(attrGroupEntity, attrGroupAndAttrsVo);
            List<AttrEntity> list = attrService.getListByGroupId(attrGroupEntity.getAttrGroupId());
            attrGroupAndAttrsVo.setAttrs(list);
            return attrGroupAndAttrsVo;
        }).collect(Collectors.toList());
        // 2. 在 group 中构建 attrs
        return collect;
    }

    /**
     * 递归获取三级分类的 id 名称
     * @param catelogId
     * @param list
     */
    private void recursionGetData(Long catelogId, List<Long> list) {
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        list.add(catelogId);
        if (categoryEntity.getParentCid() != 0) {
            recursionGetData(categoryEntity.getParentCid(),list);
        }
    }

}